from data.Datagrama import IPDatagram
from data.Datagrama import SimpleDatagram
from Util import extractNetworkIPv4


def subnetOriginStatistics(packet,mask,subnets,count):
    if packet.layer == "IP":
        subnet = extractNetworkIPv4(packet.ip_origin,mask)
        
        if subnet in subnets:
            count[subnets.index(subnet)] +=1
        else:
            subnets.append(subnet)
            count.append(1)
        
        # seboso rever isso
        subnet = extractNetworkIPv4(packet.ip_dst,mask)
        
        if subnet in subnets:
            count[subnets.index(subnet)] +=1
        else:
            subnets.append(subnet)
            count.append(1)
    
    return subnets,count

def printSubnetOriginStatistics(subnets,count):
    
    print "Subnet Statistics"
    for i in range(len(subnets)): 
        print "Subnet " +  subnets[i] + " - " + str(count[i])

def saveSubnetOriginStatistics(output,subnets,count):
    csvFile = str(output) + "-bySubnet.csv"
    csvFile = open(csvFile,"wt")
    csvFile.write("subnet,packets\n")
    for i in range(len(subnets)):
        csvFile.write(str(subnets[i])+","+str(count[i])+"\n")
    csvFile.close()

def broadcastStatistics(packet,count):
    if packet.isBroadCastIPV4():
        count+=1
    return count

def printBroadcastStatistics(packets,total):
    print "Broadcast Statistics"
    print "Broadcast packets : " + str(packets)
    print "Total : " + str(total)
    print "Percent : " + str(packets/float(total)*100) + "%"
    print "\n"
    
def saveBroadcastStatistics(output,packets,total):
    csvFile = str(output) + "-Broadcast.csv"
    csvFile = open(csvFile,"wt")
    csvFile.write("broadcast,total,% \n")
    csvFile.write(str(packets)+","+str(total)+","+str(packets/float(total)*100)+"\n")
    csvFile.close()
    
def layerRealTimeStatistics(packet,layer,count):
    
    if packet.layer in layer:
        count[layer.index(packet.layer)] += 1
    else:
        layer.append(packet.layer)
        count.append(1)
    
    return layer,count


def printLayerStatistics(layer,count,t_count):
    print "By Layer"
    
    for i in range(len(layer)):
        p = (count[i]/float(t_count)*100)
        print layer[i] , " : packets number = " , count[i] , " : " , p , "%"
    
    print "\n"

def saveLayerStatistics(output,layer,count,t_count):
    csvFile = str(output) + "-Layer.csv"
    csvFile = open(csvFile,"wt")
    for i in range(len(layer)):
        p = (count[i]/float(t_count)*100)
        csvFile.write(str(layer[i]) +","+str(count[i])+","+str(p) +"%\n")
    
    csvFile.close()    


def portStatistics(packet,ports,ports_count):
    if  packet.layer == "IP":
        if packet.port_dst in ports or packet.port_origin in ports:
            if packet.port_origin in ports:
                ports_count[ports.index(packet.port_origin)] += 1                     
            else:
                ports_count[ports.index(packet.port_dst)] += 1                
        else:
            ports.append(packet.port_dst)
            ports_count.append(1)
    
    return ports,ports_count


def printPortStatistics(ports,ports_count,total):
    
    for i in range(len(ports)):
        print ports[i] , " : packets number = " , ports_count[i] , " : " , ((ports_count[i]/float(total))*100) , "%"
    
    print "\n"

def savePortStatistics(output,ports,ports_count,total):
    csvFile = str(output) + "-IPLayer-byPort.csv"
    csvFile = open(csvFile,"wt")
    csvFile.write("PORT,PACKET COUNT,Percentage\n")
    for i in range(len(ports)):
        op =  str(ports[i])+","+str(ports_count[i])+","+str((ports_count[i]/float(total))*100)+"\n"
        csvFile.write(str(op))
    csvFile.close()

def transportLayerStatistics(packet,transport):
    
    if packet.layer == "IP":
        if packet.transport == "TCP":
            transport[0] += 1
        else:
            transport[1] += 1
        
    return transport

def printTransportLayerStatistics(transport,total):
    print "TCP/UDP"
    print "TCP : " + str(transport[0]) + "packets  , " + str((transport[0]/float(total))*100) , "%"
    print "UDP : " + str(transport[1]) + "packets  , " + str((transport[1]/float(total))*100) , "%"
    print "\n"

def saveTransportLayerStatistics(output,transport,total):
    csvFile = str(output) + "-TCP-UDP.csv"
    csvFile = open(csvFile , "wt")
    csvFile.write("TCP,"+str(transport[0]) + "," + str((transport[0]/float(total))*100)+"%\n")
    csvFile.write("UDP,"+str(transport[1]) + "," + str((transport[1]/float(total))*100)+"%\n")
    csvFile.close()